package com.yf.service.article.impl;

import com.yf.dao.article.CampaignDao;
import com.yf.dao.article.TicketDao;
import com.yf.dao.UserDao;
import com.yf.entity.CampaignEntity;
import com.yf.entity.TicketEntity;
import com.yf.entity.UserEntity;
import com.yf.enums.CampaignRestricEnum;
import com.yf.enums.CampaignStatusEnum;
import com.yf.response.RRException;
import com.yf.service.article.PlayerService;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.vo.PlayerVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import com.yf.dao.article.PlayerDao;
import com.yf.entity.PlayerEntity;

/**
 * 参与者Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Aug 22 15:16:26 CST 2018
 */
@Service
public class PlayerServiceImpl extends BaseServiceImpl<PlayerEntity> implements PlayerService {
    @Autowired
    private PlayerDao playerDao;

    @Autowired
    private CampaignDao campaignDao;

    @Autowired
    private TicketDao ticketDao;

    @Autowired
    private UserDao UserDao;



    /**内部方法**/

    /**
     * TODO 判断活动是否开启
     * @param campaignEntity
     */
    void validateCampagin(CampaignEntity campaignEntity){
        CampaignEntity campaign = campaignDao.get(campaignEntity.getId());

        long endTime = campaign.getEndTime().getTime();  //结束时间
        long currentTime = new Date().getTime();  //当前

        if(CampaignStatusEnum.CLOSE.getState().equals(campaign.getStatus()) || endTime<=currentTime){
            throw new RRException("该活动已关闭!");
        }
    }

    /**
     * TODO 判断当前用户是否给参赛者投过票
     * @param
     */
    boolean isVote(Long userId,Long campaignId){



        return false;
    }


    /**外部方法实现**/


    /**
     * 获取参赛者列表  基础信息 排名
     * @param
     * @return
     */
    @Override
    public List<PlayerVo> showPlayerList(Long campaignId){
        return playerDao.getPlayerList(campaignId);
    }

    /**
     * 获取参赛者详细信息视图  基础信息 排名
     * @param playerId
     * @return
     */
    @Override
    public PlayerVo showPlayer(Long playerId,Long userId) {
        PlayerVo playerVo = new PlayerVo();
        //获取当前登录人的ID
        playerVo.setLoginId(userId);
        //获取参赛者信息
        PlayerEntity playerEntity = playerDao.get(playerId);
        //获取排名
        Integer ranking = playerDao.ranking(playerId,playerEntity.getCampaignId());
        //获取姓名
        UserEntity userEntity = UserDao.get(playerEntity.getUserId());

        try {
            BeanUtils.copyProperties(playerVo,playerEntity);
            playerVo.setRanking(ranking);
            playerVo.setName(userEntity.getName());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //当前登录用户是否投票

        //获取活动信息
         CampaignEntity campaignEntity = campaignDao.get(playerEntity.getCampaignId());
        //一票模式
        if(CampaignRestricEnum.PER_USER.getState().equals(campaignEntity.getRestric())){
            List list1 = ticketDao.isVoteByOnlyOne(campaignEntity.getId(),userId);
            if(!list1.isEmpty()){
                playerVo.setVote(true);
            }
        }

        //一天一票模式
        if(CampaignRestricEnum.PER_DAY.getState().equals(campaignEntity.getRestric())){
            List list2 = ticketDao.isVote(campaignEntity.getId(),userId);
            if(!list2.isEmpty()){
                playerVo.setVote(true);
            }

        }

        return playerVo;
    }

    /**
     * 投票 判断活动是否开启 活动是否结束
     * @param playerId 参赛者
     * @param userId 投票人
     */
    @Override
    public PlayerVo putTicket(Long playerId, Long userId) {
        //获取参赛者的信息
        PlayerEntity playerEntity = playerDao.get(playerId);
        //获取活动信息
        CampaignEntity campaignEntity = campaignDao.get(playerEntity.getCampaignId());
        //校验活动
        validateCampagin(campaignEntity);

        //判断投票模式
        //一票模式
        if(CampaignRestricEnum.PER_USER.getState().equals(campaignEntity.getRestric())){
            List list1 = ticketDao.isVoteByOnlyOne(campaignEntity.getId(),userId);
            if(!list1.isEmpty()){
                throw new RRException("每人只能投一次");
            }
        }

        //一天一票模式
        if(CampaignRestricEnum.PER_DAY.getState().equals(campaignEntity.getRestric())){
            List list2 = ticketDao.isVote(campaignEntity.getId(),userId);
            if(!list2.isEmpty()){
                throw new RRException("每人一天只能投一次");
            }

        }

        //更新票数
        playerEntity.setTicket(playerEntity.getTicket()+1);
        playerDao.update(playerEntity);

        //保存投票记录
        TicketEntity ticketEntity = new TicketEntity();
        ticketEntity.setCreateTime(new Date());
        ticketEntity.setPlayerId(playerId);
        ticketEntity.setUserId(userId);
        ticketDao.save(ticketEntity);

        PlayerVo playerVo = new PlayerVo();

        //获取参赛者信息
        PlayerEntity playerEntity1 = playerDao.get(playerId);
        //获取排名
        Integer ranking = playerDao.ranking(playerId,playerEntity.getCampaignId());

        //投票后从新返回信息
        playerVo.setTicket(playerEntity1.getTicket());
        playerVo.setRanking(ranking);
        playerVo.setVote(true);

        return playerVo;
    }

    /**
     * 参与活动
     * 判断活动是否开启
     * 活动是否结束
     * @param userId 发起者
     * @param userId 参赛者
     * @return
     */
    @Override
    public PlayerEntity addPlayer(Long userId, Long campaginId) {
        return null;
    }

}
